home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / DrawSprocket / GoggleSprocket / GoggleSprocketTest Sources / GSpTest_EventLoop.cp < prev    next >
Encoding:
Text File  |  1998-03-12  |  5.3 KB  |  276 lines  |  [TEXT/CWIE]

  1. /*
  2. ********************************************************************************
  3. **
  4. ** File: GSpTest_EventLoop.cp
  5. **
  6. ** Description:
  7. **
  8. **    Event loop and event handler routines.
  9. **
  10. ********************************************************************************
  11. */
  12. #include "DrawSprocket.h"
  13.  
  14. #include "GSpTest_EventLoop.h"
  15.  
  16. /*
  17. ********************************************************************************
  18. ** globals
  19. ********************************************************************************
  20. */
  21. Boolean gInBackground = false;
  22. Boolean gDoneFlag = false;
  23.  
  24. /*
  25. ********************************************************************************
  26. ** local prototypes
  27. ********************************************************************************
  28. */
  29. static void UpdateMenus( void );
  30. static void DoEvent( EventRecord *inEvent );
  31. static void HandleMouseDown( EventRecord *inEvent );
  32. static void HandleMenuChoice( long inMenuChoice );
  33. static void UpdateMenus( void );
  34.  
  35. /*
  36. ********************************************************************************
  37. **
  38. ** Name: EventLoop
  39. **
  40. ** Description:
  41. **
  42. **    Program event loop.
  43. **
  44. ********************************************************************************
  45. */
  46. void EventLoop( void )
  47. {
  48.     EventRecord theEvent;
  49.         
  50.     gDoneFlag = false;
  51.     do 
  52.     {
  53.         Boolean theEventWasProcessed;
  54.         
  55.         if( WaitNextEvent( everyEvent, &theEvent, 0, NULL ) )
  56.         {
  57.             DSpProcessEvent( &theEvent, &theEventWasProcessed );
  58.             if( false == theEventWasProcessed )
  59.                 DoEvent( &theEvent );
  60.         }
  61.     } while ( false == gDoneFlag );
  62. }
  63.  
  64. /*
  65. ********************************************************************************
  66. **
  67. ** Name: DoEvent
  68. **
  69. ** Description:
  70. **
  71. **    Process an event.
  72. **
  73. ********************************************************************************
  74. */
  75. static void
  76. DoEvent(
  77.     EventRecord *inEvent
  78. )
  79. {
  80.     switch( inEvent->what ) 
  81.     {
  82.         case nullEvent:
  83.             break;
  84.             
  85.         case mouseDown:
  86.             HandleMouseDown( inEvent );
  87.             break;
  88.                             
  89.         case mouseUp:
  90.             break;
  91.                             
  92.         case keyDown:
  93.         case autoKey:
  94.             // check for menu key equivalents
  95.             if( inEvent->modifiers & cmdKey )
  96.                 HandleMenuChoice( MenuKey( inEvent->message & charCodeMask ) );
  97.             break;
  98.                             
  99.         case updateEvt:
  100.             // refresh contexts here
  101.             break;
  102.                             
  103.         case diskEvt:
  104.             if( HiWord( inEvent->message ) != noErr ) 
  105.             {
  106.                 Point thePoint;
  107.  
  108.                 SetPt( &thePoint, 50, 50) ;
  109.                 DIBadMount( thePoint, inEvent->message );
  110.             }
  111.             break;
  112.                             
  113.         case osEvt:
  114.             switch( ( inEvent->message >> 24 ) & 0xFF ) 
  115.             {        
  116.                 case suspendResumeMessage:    
  117.                     gInBackground = inEvent->message & resumeFlag;
  118.                     break;
  119.             }
  120.             break;
  121.     
  122.         case kHighLevelEvent:
  123.             AEProcessAppleEvent( inEvent );
  124.             break;
  125.     }
  126.  
  127. }            
  128.  
  129. /*
  130. ********************************************************************************
  131. **
  132. ** Name: HandleMouseDown
  133. **
  134. ** Description:
  135. **
  136. **    Process mouse down event.
  137. **
  138. ********************************************************************************
  139. */
  140. static void HandleMouseDown(
  141.     EventRecord *inEvent        // event record
  142. )
  143. {
  144.     short            thePart;
  145.     WindowRef        theWindow;
  146.     long            theMenuChoice;
  147.  
  148.     thePart = FindWindow( inEvent->where, &theWindow);
  149.  
  150.     // if the event occured in a window that is not the front window,
  151.     // then select the window and throw away the event
  152.     if( theWindow && theWindow != FrontWindow() )
  153.     {
  154.         SelectWindow( theWindow );
  155.         return;
  156.     }
  157.             
  158.     switch( thePart )
  159.     {
  160.         case inMenuBar:
  161.  
  162.             UpdateMenus();
  163.  
  164.             theMenuChoice = MenuSelect( inEvent->where );
  165.             if( HiWord( theMenuChoice ) )            
  166.                 HandleMenuChoice( theMenuChoice );
  167.             break;
  168.  
  169.         case inContent:
  170.             break;
  171.  
  172.         case inSysWindow:
  173.             SystemClick( inEvent, theWindow );
  174.             break;
  175.                                                 
  176.         case inDrag:
  177.             DragWindow( theWindow, inEvent->where, &qd.screenBits.bounds );
  178.             break;
  179.                         
  180.         case inGoAway:
  181. //            if( TrackGoAway( theWindow, inEvent->where ) )
  182.             break;
  183.                         
  184.         case inZoomIn:
  185.         case inZoomOut:
  186. //            if( TrackBox( theWindow, inEvent->where, thePart ) ) 
  187.             break;
  188.                         
  189.     }
  190. }
  191.  
  192. /*
  193. ********************************************************************************
  194. **
  195. ** Name: HandleMenuChoice
  196. **
  197. ** Description:
  198. **
  199. **    Process menu event.
  200. **
  201. ********************************************************************************
  202. */
  203. static void
  204. HandleMenuChoice(
  205.     long inMenuChoice
  206. )
  207. {
  208.     short theItem, theMenu;
  209.     Str255 theDeskAccessoryName;
  210.  
  211.     Rect        bounds;
  212.     WindowRef    newWindow;
  213.  
  214.     theItem = LoWord( inMenuChoice );
  215.     theMenu = HiWord( inMenuChoice );
  216.  
  217.     switch( theMenu ) 
  218.     {
  219.         case kMenu_Apple:
  220.             switch( theItem ) 
  221.             {
  222.                 case kMenuItem_About:
  223.                     break;
  224.                     
  225.                 default:
  226.                     GetMenuItemText( GetMenuHandle( kMenu_Apple ), theItem,
  227.                         theDeskAccessoryName );
  228.                     OpenDeskAcc( theDeskAccessoryName );
  229.                     break;
  230.             }    
  231.             break;
  232.                     
  233.         case kMenu_File:
  234.             switch( theItem ) 
  235.             {
  236.                 case kMenuItem_New:
  237.                     break;
  238.                     
  239.                 case kMenuItem_Open:
  240.                     break;
  241.                         
  242.                 case kMenuItem_Close:
  243.                     break;    
  244.                         
  245.                 case kMenuItem_Quit:
  246.                     gDoneFlag = true;
  247.                     break;
  248.                     
  249.                 default:
  250.                     break;    
  251.             }
  252.             break;
  253.         
  254.         default:
  255.             break;    
  256.         
  257.     }
  258.     
  259.     HiliteMenu(0);
  260. }
  261.  
  262. /*
  263. ********************************************************************************
  264. **
  265. ** Name: UpdateMenus
  266. **
  267. ** Description:
  268. **
  269. **    Update menus to reflect correct settings.  Called just before the
  270. **    menu bar is displayed.
  271. **
  272. ********************************************************************************
  273. */
  274. static void UpdateMenus( void )
  275. {
  276. }